USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0004_NRO_CUOTAS]    Script Date: 11/25/2011 11:56:02 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[WASP_M0004_NRO_CUOTAS]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[WASP_M0004_NRO_CUOTAS]
GO

USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0004_NRO_CUOTAS]    Script Date: 11/25/2011 11:56:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[WASP_M0004_NRO_CUOTAS] 

AS
BEGIN
--OBTIENES LOS CREDITOS
	SELECT AOCTA, AOOPER, AOSUC, NULL AS CUOTAS
	INTO  dbo.CUOTAS_AUX
	FROM 
		FSD601 ,
		FSD010 
	WHERE 
		FSD601.PPCTA = FSD010.AOCTA  AND 
		FSD601.PPOPER = FSD010.AOOPER AND
		FSD601.PPSUC = FSD010.AOSUC AND
		FSD601.PPTOPE =FSD010.AOTOPE AND
		FSD601.D601CO = 'S' AND 
		FSD601.PPOPER <> 999999999 AND 
		FSD601.PPSUC<>0 AND
		FSD010.AOSTAT<>99
	GROUP BY AOCTA, AOOPER, AOSUC

---CALCULA LAS CUOTAS
	SELECT FSD601.PPSUC,FSD601.PPCTA,FSD601.PPOPER, COUNT(FSD601.PPOPER)AS CUOTAS
	INTO DBO.CUOTAS_AUX2
	FROM FSD601
	WHERE FSD601.PPSBOP < 100  AND FSD601.PPSTAT <> '99' AND FSD601.PPSUC <> 0 AND FSD601.D601CO = 'S' AND FSD601.PPOPER <> 999999999
	GROUP BY FSD601.PPSUC, FSD601.PPCTA, FSD601.PPOPER

--ACTUALIZA LAS CUOTAS
	UPDATE DBO.CUOTAS_AUX
	SET DBO.CUOTAS_AUX.CUOTAS = DBO.CUOTAS_AUX2.CUOTAS
	FROM DBO.CUOTAS_AUX2
	WHERE 
		DBO.CUOTAS_AUX.AOCTA = DBO.CUOTAS_AUX2.PPCTA AND 
		DBO.CUOTAS_AUX.AOOPER = DBO.CUOTAS_AUX2.PPOPER AND
		DBO.CUOTAS_AUX.AOSUC= DBO.CUOTAS_AUX2.PPSUC 
END

GO


